home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / sorts / testsort.bas < prev   
Encoding:
BASIC Source File  |  1995-09-06  |  2.7 KB  |  101 lines

  1.  
  2. '   This is the test case for SortStrings()
  3. '
  4. '   The test output will show up in the immediate
  5. '   panel.
  6.  
  7. '   The test program generates an array of strings
  8. '   filled with short strings of random characters
  9. '   in the range "A" to "Z", in both upper case and
  10. '   lower case. It then sorts them in ascending and
  11. '   descending order, first respecting the letters'
  12. '   case then ignoring it (sorting lexically).
  13.  
  14. '   Remember that upper case letters A-Z all appear
  15. '   before the lower case letters, so if you respect
  16. '   the case of the strings, "Z" sorts before "a".
  17.  
  18. Function TestSortRandomInt (ByVal lowerBound As Integer, ByVal upperBound As Integer) As Integer
  19. '
  20. '   Returns a random integer in specified range.
  21. '   The range includes the end points.
  22. '
  23.    TestSortRandomInt = Int((upperBound - lowerBound + 1) * Rnd + lowerBound)
  24.  
  25. End Function
  26.  
  27. Sub TestSorts ()
  28. '
  29. '   Runs the test case for SortStrings()
  30. '
  31.     Const ARRAYSIZE = 5         'ubound of the test array
  32.     Const LOWINDEX = 0          'first array index for sort
  33.     Const HIGHINDEX = ARRAYSIZE 'last array index for sort
  34.  
  35.     Dim firstCode As Integer
  36.     Dim lastCode  As Integer
  37.     Dim thisChar  As String
  38.     ReDim a(ARRAYSIZE) As String
  39.  
  40.     Randomize Timer         'seed the random number generator
  41.  
  42.     firstCode = Asc("A")
  43.     lastCode = Asc("Z")
  44.  
  45.     For i = LOWINDEX To HIGHINDEX
  46.         thisChar = Chr$(TestSortRandomInt(firstCode, lastCode))
  47.         If TestSortRandomInt(0, 1) = 1 Then     'about half the time,
  48.             thisChar = LCase$(thisChar)         '   change case
  49.         End If
  50.         a(i) = String$(8, thisChar)             'make a short string of char
  51.     Next i
  52.  
  53.     Debug.Print ""
  54.     Debug.Print "Initially:"
  55.     For i = LOWINDEX To HIGHINDEX
  56.         Debug.Print a(i)
  57.     Next i
  58.     Debug.Print ""
  59.  
  60.     Debug.Print "Ascending sort:"
  61.     Debug.Print ""
  62.  
  63.     SortStrings a(), LOWINDEX, HIGHINDEX, SORTASCENDING
  64.  
  65.     Debug.Print "Respecting case:"
  66.     For i = LOWINDEX To HIGHINDEX
  67.         Debug.Print a(i)
  68.     Next i
  69.     Debug.Print ""
  70.  
  71.     SortStrings a(), LOWINDEX, HIGHINDEX, SORTASCENDING Or SORTIGNORECASE
  72.  
  73.     Debug.Print "Ignoring case:"
  74.     For i = LOWINDEX To HIGHINDEX
  75.         Debug.Print a(i)
  76.     Next i
  77.     Debug.Print ""
  78.  
  79.     Debug.Print ""
  80.     Debug.Print "Descending sort:"
  81.     Debug.Print ""
  82.  
  83.     SortStrings a(), LOWINDEX, HIGHINDEX, SORTDESCENDING
  84.  
  85.     Debug.Print "Respecting case:"
  86.     For i = LOWINDEX To HIGHINDEX
  87.         Debug.Print a(i)
  88.     Next i
  89.     Debug.Print ""
  90.  
  91.     SortStrings a(), LOWINDEX, HIGHINDEX, SORTDESCENDING Or SORTIGNORECASE
  92.  
  93.     Debug.Print "Ignoring case:"
  94.     For i = LOWINDEX To HIGHINDEX
  95.         Debug.Print a(i)
  96.     Next i
  97.     Debug.Print ""
  98.  
  99. End Sub
  100.  
  101.